package jh.com.jhappbf.utils;

import android.os.Environment;
import android.util.Log;

import com.blankj.utilcode.util.FileUtils;

import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;

import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * Created by Administrator on 2018/7/25.
 */

public class FileImport {
    private static String xlsFilePath = Environment.getExternalStorageDirectory()
            + "/outxmldata/";

    public static boolean exportExcel(String tempName, ArrayList<HashMap<String, String>> lists2) {

        try {

            String file;
            if (tempName.isEmpty()) {
                file = xlsFilePath + "xls" + GetTimesyyyymmddhhmmss() + ".xls";
            } else {
                file = xlsFilePath + tempName;
            }
            File path2 = new File(xlsFilePath);

            if (path2.mkdirs()) {

            }
            List<Object> al22 = new ArrayList<>();
            List<String> al2 = new ArrayList<>();
            al2.add("编号");

            // al2.add("筛选栏");

            al22.add(al2);
            FileXls.writeXLS(file, al22);
            List<Object> ac = new ArrayList<>();

            String id = "";
            // String sxl = "";
            for (int i = 0; i < lists2.size(); i++) {
                List<String> al = new ArrayList<>();
                Set<Entry<String, String>> sets = lists2.get(i).entrySet();
                for (Entry<String, String> entry : sets) {
                    if ("tagUii".equals(entry.getKey())) {
                        id = entry.getValue();
                    }
                    // Object value=entry.getValue();
                }
                al.add(id);

                // al.add(sxl);
                ac.add(al);
            }

            return FileXls.writeXLS(file, ac);
        } catch (Exception ex) {
            Log.i("导出异常", ex.getMessage());
            return false;
        }
    }

    public static boolean generateExcel(String fileName, List<JSONObject> objects, List<String> fields, List<String> columnNames) {
        String name = xlsFilePath + fileName + GetTimesyyyymmddhhmmss() + ".xls";
        boolean result = FileUtils.createOrExistsFile(name);
        File file = null;
        if (result) {
            file = FileUtils.getFileByPath(name);
        }
        if (file == null) {
            return false;
        }
        try {
            //声明工作簿jxl.write.WritableWorkbook
            WritableWorkbook wwb;
            wwb = Workbook.createWorkbook(file);
            /*
             * 创建一个工作表、sheetName为工作表的名称、"0"为第一个工作表
             * 打开Excel的时候会看到左下角默认有3个sheet、"sheet1、sheet2、sheet3"这样
             * 代码中的"0"就是sheet1、其它的一一对应。
             * createSheet(sheetName, 0)一个是工作表的名称，另一个是工作表在工作薄中的位置
             */
            WritableSheet ws = wwb.createSheet("sheet1", 0);

            SheetSettings ss = ws.getSettings();
            ss.setVerticalFreeze(1);//冻结表头
            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
            WritableFont font2 = new WritableFont(WritableFont.createFont("微软雅黑"), 9, WritableFont.NO_BOLD);
            WritableCellFormat wcf = new WritableCellFormat(font1);
            WritableCellFormat wcf2 = new WritableCellFormat(font2);
            WritableCellFormat wcf3 = new WritableCellFormat(font2);//设置样式，字体

            //创建单元格样式
            //WritableCellFormat wcf = new WritableCellFormat();

            //背景颜色
            wcf.setBackground(jxl.format.Colour.YELLOW);
            wcf.setAlignment(Alignment.CENTRE);  //平行居中
            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中
            wcf3.setAlignment(Alignment.CENTRE);  //平行居中
            wcf3.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中
            wcf3.setBackground(Colour.LIGHT_ORANGE);
            wcf2.setAlignment(Alignment.CENTRE);  //平行居中
            wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中

            /*
             * 这个是单元格内容居中显示
             * 还有很多很多样式
             */
            wcf.setAlignment(Alignment.CENTRE);
            if (columnNames != null && columnNames.size() > 0) {
                //循环写入表头
                for (int i = 0; i < columnNames.size(); i++) {

                    /*
                     * 添加单元格(Cell)内容addCell()
                     * 添加Label对象Label()
                     * 数据的类型有很多种、在这里你需要什么类型就导入什么类型
                     * 如：jxl.write.DateTime 、jxl.write.Number、jxl.write.Label
                     * Label(i, 0, columns[i], wcf)
                     * 其中i为列、0为行、columns[i]为数据、wcf为样式
                     * 合起来就是说将columns[i]添加到第一行(行、列下标都是从0开始)第i列、样式为什么"色"内容居中
                     */
                    ws.addCell(new Label(i, 0, columnNames.get(i), wcf));
                }

                //判断表中是否有数据
                if (objects != null && objects.size() > 0) {
                    //循环写入表中数据
                    for (int row = 0; row < objects.size(); row++) {
                        for (int column = 0; column < fields.size(); column++) {
                            //括号中第一个参数代表列，第二个参数代表行，第三个参数代表内容
                            //下面第一个也就是第0列的第1行（第0行做了表头）。
                            ws.addCell(new Label(column, row + 1, objects.get(row).optString(fields.get(column))));
                        }

                    }

                }
                //写入Exel工作表
                wwb.write();

                //关闭Excel工作薄对象
                wwb.close();
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    public static String getTimeyyyymmdd() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String dt = formatter.format(curDate);

        return dt;

    }

    public static String GetTimesddMMyy() {

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy", Locale.CHINA);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String dt = formatter.format(curDate);

        return dt;

    }

    public static String GetTimesyyyymmddhhmmss() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String dt = formatter.format(curDate);

        return dt;

    }
}
